package view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;

import model.EmailImp;

import requestManager.EmailApplicationRequestManagerImp;
import resources.ResourceProvider;

/**
 * This is a class to display JFrame to represent Email Application i.e how a client will view it.
 */
public class EmailApplicationView extends JFrame implements EmailApplicationViewImp
{
   
    
	private static final long serialVersionUID = 2562300576978868891L;
	
	private LoginPage login;
	
	private RegisterPage register;
	
	private EmailApplicationRequestManagerImp emailAppController;

    private MailDisplayArea displayArea;
	
	private FolderHierarchyPanel foldHierarchyPanel;
	
	private OptionButtonArea optionButtonArea;
	
	private JSplitPane mainSplitPane;
				
    /**
     * Constructor for class EmailApplication view.
     */
    public EmailApplicationView() 
    {     
       super(ResourceProvider.APPLICATION_NAME);
       this.setVisible(true);
       this.setSize(900, 700);
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       this.setLocationRelativeTo(null); 
    }
    
    /**Initially after logging in, the user will see the file menu having exit, compose new mail option and folder 
     * hierarchy for mailbox, option buttons and mail display area.
     */
    public void initialize()
    {
        this.reset();
        
        JMenuBar menubar = new JMenuBar();
        
        JMenu application = new JMenu("Application");
        application.setVisible(true);
        menubar.add(application);
        
        JMenu View = new JMenu("View");
        View.setVisible(true);
        menubar.add(View);       
        this.setJMenuBar(menubar);
                
        JMenuItem exit = new JMenuItem("Exit");
        exit.addActionListener(new ActionListener()
        {
            /*
             * (non-Javadoc)
             * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
             */
            public void actionPerformed(ActionEvent arg0)
            {
                int isExit = JOptionPane.showConfirmDialog(EmailApplicationView.this, "Are you sure to want to exit?", "Warning",
                    JOptionPane.YES_NO_OPTION);

                if(isExit == JOptionPane.OK_OPTION)
                {
                   System.exit(0);
                }                  
            }
        });
        
        JMenuItem newmail=new JMenuItem("Compose new Mail");
        newmail.addActionListener(new ActionListener()
        {
            /*
             * (non-Javadoc)
             * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
             */
            public void actionPerformed(ActionEvent arg0)
            {
                EmailApplicationView.this.displayArea.displayMessagePanel(); 
               EmailApplicationView.this.optionButtonArea.displayButtonsForNewMessage();
          
            }
        }); 
        
        JMenuItem logOut=new JMenuItem("Log Out");
        logOut.addActionListener(new ActionListener()
        {
            /*
             * (non-Javadoc)
             * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
             */
            public void actionPerformed(ActionEvent arg0)
            {
                int Logout = JOptionPane.showConfirmDialog(EmailApplicationView.this, "Are you sure to log out?", "Warning", JOptionPane.YES_NO_OPTION);
                if(Logout == JOptionPane.OK_OPTION)
                {
                      SwingUtilities.invokeLater(new Runnable() {
                          
                          /*
                           * (non-Javadoc)
                           * @see java.lang.Runnable#run()
                           */
                          public void run() 
                          {
                              EmailApplicationView.this.clearAllAndDisplayLogin();;
                          }
                      });
                }
            }
        }); 
        
        application.add(newmail);
        application.add(logOut);
        application.add(exit);
        
        JMenuItem template = new JMenuItem("Template");
        template.addActionListener(new ActionListener()
        {
        	public void actionPerformed(ActionEvent teme)
        	{
        		EmailApplicationView.this.optionButtonArea.displayButtonsForTemplates();
        		EmailApplicationView.this.displayArea.reset();
        	}
        });
        
        JMenuItem filter = new JMenuItem("Filter");
        filter.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent teme)
            {
                EmailApplicationView.this.optionButtonArea.displayButtonsForFilters();
                EmailApplicationView.this.displayArea.reset();
            }
        });
        
        View.add(template);
        View.add(filter);

        foldHierarchyPanel = new FolderHierarchyPanel(this);
        foldHierarchyPanel.initialize();
        foldHierarchyPanel.loadPersistenceFolders();   

        
        optionButtonArea = new OptionButtonArea(this);
        optionButtonArea.initialize();
        
        displayArea = new MailDisplayArea(this);
        displayArea.setLayout(new BorderLayout(10, 10));
        
        JSplitPane right = new JSplitPane(JSplitPane.VERTICAL_SPLIT, optionButtonArea, displayArea);
        right.setDividerLocation(90);
       
        mainSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, foldHierarchyPanel,right);
        mainSplitPane.setDividerLocation(200);
        
        this.add(mainSplitPane);
        
        optionButtonArea.registerObserver(displayArea);
        
        this.revalidate();
        this.repaint();
        
    }

    /**
     * reset frame
     */
    private void reset()
    {
        if(this.login != null)
        {
           this.remove(this.login);       
        }
        
        if(this.register != null)
        {
           this.remove(this.register);       
        }
    }
    
    /**
     * clear all the components inside the frame and dispaly login UI
     */
    public void clearAllAndDisplayLogin()
    {
        this.remove(mainSplitPane);
        this.setJMenuBar(null);
        
        this.displayLogin();
    }

    /**
     * display login UI
     */
    public void displayLogin()
    { 
        this.reset();
        this.login = new LoginPage(this);
        this.add(this.login);
        this.revalidate();
        this.repaint();
    }

    /**
     * display register UI
     */
    public void displayRegister()
    {
        this.reset();
        this.register = new RegisterPage(this);
        this.add(this.register);
        this.revalidate();
        this.repaint();
    }

    /*
     * (non-Javadoc)
     * @see view.EmailApplicationViewImp#registerController(requestManager.EmailApplicationRequestManagerImp)
     */
    public void registerController(EmailApplicationRequestManagerImp emailAppController)
    {
        this.emailAppController = emailAppController;
    }
    
    /*
     * (non-Javadoc)
     * @see view.EmailApplicationViewImp#unRegisterController()
     */
    public void unRegisterController()
    {
        this.emailAppController = null;
    }
    
    /*
     * (non-Javadoc)
     * @see view.EmailApplicationViewImp#getController()
     */
    public EmailApplicationRequestManagerImp getController()
    {
        return this.emailAppController;
    }
    
    /**
     * display in the main display area for the given path
     * @param path the path of the root folder
     */
    public void displayInMailDisplayArea(String path)
    {
        this.displayArea.displayEmails(path);
    }

    /**
     * get mail display area panel reference
     * @return returns the display area.
     */
    public MailDisplayArea getMailDisplayArea()
    {
        return this.displayArea;
    }
    
    /**
     * get folder hierarchy panel reference
     * @return returns the folder hierarchy panel.
     */
    public FolderHierarchyPanel getFoldHierarchyPanel()
    {
        return this.foldHierarchyPanel;
    }

    /**
     * display in button area of the given path
     * @param persistenceFilePath the persistence file path
     */
    public void displayInButtonArea(String persistenceFilePath)
    {
        this.optionButtonArea.displayButtons(persistenceFilePath);      
    }
    
    /**
     * get reference of button display area
     * @return returns the option button area.
     */
    public OptionButtonArea getButtonDisplayArea()
    {
        return this.optionButtonArea;
    }
   
    /*
     * (non-Javadoc)
     * @see view.EmailApplicationViewImp#getEmailFromJasonFile(java.io.File)
     */
    public EmailImp getEmailFromJasonFile(File file)
    {
    	return this.getController().requestEmailFromJson(file.getPath());
    }
    
}
